home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 8263 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  2.7 KB

  1. Path: inferno.mpx.com.au!news
  2. From: Malcolm Smith <mjsmith@mpx.com.au>
  3. Newsgroups: comp.lang.pascal.misc,comp.lang.c++,comp.lang.c,comp.lang.pascal.borland
  4. Subject: Re: Tough FACTORIAL math problem...
  5. Date: 16 Feb 1996 07:46:59 GMT
  6. Organization: Microplex Pty Ltd
  7. Message-ID: <4g1cpj$3dd@inferno.mpx.com.au>
  8. References: <4fr8be$ass@news.iconn.net>
  9. NNTP-Posting-Host: dialup-211.mpx.com.au
  10.  
  11. thecrow@iconn.net (The Crow) wrote:
  12. >
  13. > Here is what I am trying to do, I want to find the last NON-ZERO digit of a 
  14. > given factorial.  For instance,
  15. > 5! = 120
  16. > so the last non-zero digit is 2.  I want to be able to do this up to 1000.  
  17. > Problem is, no matter how huge of a data-type you use, there isn't any way for 
  18. > the computer to handle 1000!, it is however possible to find the last non-zero 
  19. > digit somehow.  One thing I have tried is as I went through mulitplying the 
  20. > series of numbers in the factorial (5 * 4 * 3 * 2) I would remove all the 
  21. > trailing ZEROS, I got this to work up to 789, but it wont work with 1000 and i 
  22. > am not really sure why.  If anyone has a clue how I can do this let me know.
  23. > -- 
  24. > The Crow - thecrow@iconn.net
  25. > "It can't rain all the time"
  26. > -Kryptology
  27.  
  28.  
  29. Why not treat the numbers as strings.  There is a technique of
  30. multiplying numbers using a table.
  31.  
  32. Imagine multiplying 7658 x 453.  Form a table 4 x 3.
  33.  
  34.  
  35.  7  6  5  8
  36. +----------+
  37. |          | 4
  38. |          | 5
  39. |          | 3
  40. +----------+  
  41.  
  42. Draw horizontal and vertical lines for each column and row.  Next draw
  43. diagonal lines (/) between each sqaure.  Multiply each number (column
  44. by row) and insert the answers in each half of each sqaure.
  45.  
  46.             6
  47. e.g.,     +---+
  48.           |1 /|
  49.           | / | 3  
  50.           |/ 8|
  51.           +---+
  52.  
  53. When the table is complete you add the diagonal columns (from right
  54. to left) putting the last digit under the corresponding column and
  55. carry the remainder of the answer into the next column (to the left).
  56. (if the total was 156 put a 6 under the column and add 15 to the next
  57. diagonal on the left).
  58.  
  59. When you have finished adding the diagonal columns you will find the
  60. answer from left to right around the table.
  61.  
  62.        5   6
  63.      +---+---+
  64.      |1 /|1 /|
  65.    1 | / | / | 3          The answer is 168.
  66.      |/ 5|/8 |
  67.      +-------+
  68.        6   8
  69.  
  70.  
  71. You could treat all your numbers as strings, muliply them by using
  72. an array (or to save on memory you could evaluate each column as you
  73. go.
  74.  
  75. I don't have any code handy.  If you want some Email me and I'll try
  76. to find some time.  State whether you want to use an array or memory.
  77. The memory version will probably be slower because as the numbers
  78. grow it will be re-evaluating columns many times over.
  79.  
  80.  
  81.  
  82. Mal
  83. mjsmith@mpx.com.au
  84.  
  85.